package old.gui;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme3.app.Application;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial.CullHint;
import com.jme3.system.AppSettings;


/*
 * A space on which other Windows or components may be placed.  Windows are
 * transparent, rectangular, and their child Windows may not overlap. 
 */
public class Window extends Node {
	
	private int width;
	private int height;
		
	//Pixel location of upper left corner of window
	private int refPointX;
	private int refPointY;
	
	private static final Logger logger = Logger.getLogger(Application.class.getName());
	
	//Level that window is currently at.  Used to determine overlap priority
	//e.g. a 3 will display over a 2.  Windows all start on 0, are auto-promoted
	//by GUI
	private int overlapLevel = 0;
	
	public Window(String name, int width, int height)
	{
		super(name);		
		initialize(width, height);
	}
	
	private void initialize(int width, int height)
	{
		setQueueBucket(Bucket.Gui);
		setCullHint(CullHint.Never);
		
		resize(width, height);				
	}
	
	public int getWidth()
	{return this.width;}
	
	public int getHeight()
	{return this.height;}
	
	public int getRefPointX()
	{return this.refPointX;}
	
	public int getRefPointY()
	{return this.refPointY;}
	
	public void resize(int width, int height)
	{
		this.width = width;
		this.height = height;
	}
	
	public void setOverlapLevel(int level)
	{this.overlapLevel = level;}
	
	public int getOverlapLevel()
	{return this.overlapLevel;}

	//utility method, checks for overlap between any child windows
	//Returns false if there are problems.
	public boolean testBounds()
	{
		int size = children.size();
		
		if (children.size()==0)
		{return true;}
		
		int width1 = 0;
		int width2 = 0;
		int height1 = 0;
		int height2 = 0;
		int refX1 = 0;
		int refX2 = 0;
		int refY1 = 0;
		int refY2 = 0;
		boolean flag = true;
		boolean finalFlag = true;
		Window rect1;
		Window rect2;		
		
		for (int i = 0; i < size; i++)
		{
			rect2 = ((Window) children.get(i));
			
			for (int j = i+1; j< size; j++)
			{
				rect1 = ((Window) children.get(j));
				
				width1 = rect1.getWidth();
				height1 = rect1.getHeight();
				refX1 = rect1.getRefPointX();
				refY1 = rect1.getRefPointY();
				
				width2 = rect2.getWidth();
				height2 = rect2.getHeight();
				refX2 = rect2.getRefPointX();
				refY2 = rect2.getRefPointY();
				
				
				flag = //rect1 is completely to the left of rect2 
					((refX1 < refX2) && ((refX1+width1) < refX2)) || 
					//rect1 is completely to the right of rect2
					((refX1 > (refX2+width2))) ||
					//rect1 is completely above rect2
					((refY1 < refY2) && ((refY1+height1) < refY2)) ||
					//rect1 is completely below rect2
					((refY1 > (refY2+height2)));
				
				if (!flag)
				{
					finalFlag = false;
					logger.log(Level.FINE, "Window.testbounds(), " + this.name + "; "
							+ rect1.getName() +	" " + rect2.getName() + " intersect.",
							getClass().getName());					
				}
			}
		}
		
		return finalFlag;
	}
	
	

}


//private RefAlignment alignment;
//
////refPoint location on Window
//private enum RefAlignment {
//	CENTER,
//	UPPER_LEFT,
//	UPPER_RIGHT,
//	LOWER_LEFT,
//	LOWER_RIGHT;
//}

//public void setAlignment(RefAlignment alignment)
//{
//	this.alignment = alignment;
//}
